package com.biz.primus.ms.oms.dao.specification;

import com.biz.primus.common.utils.StringUtil;
import com.biz.primus.model.oms.vo.OrderQueryVo;
import com.biz.primus.model.oms.vo.OrderRefundQueryVo;
import com.biz.primus.model.oms.vo.OrderReturnQueryVo;
import com.biz.primus.ms.oms.dao.po.returns.OmsRefund;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturn;
import com.google.common.collect.Lists;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @author LGJ
 * @date
 */
public class OmsOrderRefundSpecification implements Specification<OmsRefund> {

    private OrderRefundQueryVo orderQueryVo;

    public OmsOrderRefundSpecification(OrderRefundQueryVo orderQueryVo){
        this.orderQueryVo=orderQueryVo;
    }

    @Override
    public Predicate toPredicate(Root<OmsRefund> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> predicates = Lists.newArrayList();

        if(!StringUtil.isNullOrEmpty(orderQueryVo.getRefundCode())){
            predicates.add(cb.equal(root.get("refundCode").as(String.class),orderQueryVo.getRefundCode()));
        }

        query.where(predicates.toArray(new Predicate[predicates.size()]));
        return query.getRestriction();
    }
}
